﻿using System;
using System.Threading.Tasks;
using Config;
using IoGame.Gen;
using UnityEngine;

namespace Wxy.Net
{
    public class NetSystem : MonoBehaviour
    {
        private static NetSystem _instance;

        public static NetSystem Instance => _instance;

        private void Start()
        {
            Debug.Log("NetSystem Start");
            string ip = "192.168.3.46";
            string port = "10100";
            MyNetConfig.StartNet(ip, port, () => { SendCreateRoomInfo(); });
        }

        private void OnDestroy()
        {
            Debug.Log("NetSystem Stop");
            MyNetConfig.StopNet();
        }

        public async Task<bool> SendCreateRoomInfo()
        {
            ServerCmdConfig serverCmdConfig = new ServerCmdConfig();
            serverCmdConfig.Cmd = EServerCmd.CreateRoomInfo;
            serverCmdConfig.Info = "i want to create room";
            var json = JsonUtility.ToJson(serverCmdConfig);
            var result = await SdkAction.OfAwaitStringValue(json);
            return result.Success();
        }

        public async Task<bool> SendExitRoomInfo()
        {
            ServerCmdConfig serverCmdConfig = new ServerCmdConfig();
            serverCmdConfig.Cmd = EServerCmd.ExitRoomInfo;
            serverCmdConfig.Info = "i want to exit room";
            var json = JsonUtility.ToJson(serverCmdConfig);
            var result = await SdkAction.OfAwaitStringValue(json);
            return result.Success();
        }
    }
}